package Action;

import java.util.List;

import com.opensymphony.xwork2.ActionSupport;

import Beans.Agent;
import Beans.Company;
import Beans.Courier;
import Beans.Expense;
import Beans.Express;
import DAO.AgentDao;
import DAO.BranchDao;
import DAO.CompanyDao;
import DAO.CourierDao;
import DAO.ExpenseDao;
import DAO.ExpressDao;

@SuppressWarnings("serial")
public class ExpenseAction extends ActionSupport {

	private List<Expense> expenses;
	private List<Express> expresses;
	private List<Company> companies;

	ExpressDao expressDao = new ExpressDao();
	BranchDao branchDao = new BranchDao();
	CompanyDao companyDao = new CompanyDao();
	ExpenseDao expenseDao = new ExpenseDao();
	AgentDao agentDao = new AgentDao();

	private int branchID;
	private int agentID;
	private int expenseID;
	private int expressId;
	private int company;
	private Expense expense = new Expense();

	/**
	 * 编辑扣费信息
	 * 
	 * @return
	 */
	public String editExpense() {
		Express express=expressDao.getExpressBranch(expressId);
		express.setStatus(1);
		expressDao.edit(express);
		Expense e=expenseDao.getExpenseById(expenseID);
		expense.setExpenseId(expenseID);
		expense.setExpress(express);
		expense.setBranch(branchDao.getBranchById(branchID));
		expense.setChecked(e.getChecked());
		expenseDao.edit(expense);
		return "success";
	}

	/**
	 * 删除扣费信息
	 * 
	 * @return
	 */
	public String deleteExpense() {
		expense = expenseDao.getExpenseById(expenseID);
		Express express=expense.getExpress();
		express.setStatus(0);
		expressDao.edit(express);
		expenseDao.delete(expense);
		return "success";
	}
	/**
	 * 通过审核
	 * 
	 * @return
	 */
	public String passUncheckedExpense() {
		expense=expenseDao.getExpenseById(expenseID);
		expense.setChecked(1);
		expenseDao.edit(expense);
		return "success";
	}
	/**
	 * 删除申请
	 * 
	 * @return
	 */
	public String deleteUncheckedExpense() {
		expense.setExpenseId(expenseID);
		expenseDao.delete(expense);
		return "success";
	}

	/**
	 * 新增扣费信息
	 * 
	 * @return
	 */
	public String registerExpense() {
		Express express=expressDao.getExpressBranch(expressId);
		express.setStatus(1);
		expressDao.edit(express);
		expense.setExpress(express);
		expense.setBranch(branchDao.getBranchById(branchID));
		expense.setChecked(1);
		if(expenseDao.checkSize(expense).size()<1){
			expenseDao.save(expense);
		}
		return "success";
	}

	/**
	 * 通过ID获取扣费信息
	 */
	public String getExpenseById() {
		addExpense();
		expense = expenseDao.getExpenseById(expenseID);
		Express express=expense.getExpress();
		express.setStatus(0);
		expressDao.edit(express);
		return "success";
	}

	/**
	 * 获取所有扣费信息
	 */
	public String getAllExpense() {
		expenses = expenseDao.getAllExpense(branchDao.getBranchById(branchID));
		return "success";
	}
	
	/**
	 * 获取所有扣费信息
	 */
	public String getAllUncheckedExpense() {
		expenses = expenseDao.getAllExpense();
		return "success";
	}

	/**
	 * 获取快递员的网点和公司
	 * 
	 * @return
	 */
	public String getBranchAndCompany() {
		companies = companyDao.getAllcompany();
		if (company != 0) {
			expresses = expressDao.getCompanyExpressBranch(company);
			return "jsonSuccess";
		} else {
			return "success";
		}
	}
	
	/**
	 * 获取快递员的网点和公司
	 * 
	 * @return
	 */
	public String addExpense() {
		companies = companyDao.getAllcompany();
		Agent agent=agentDao.getAgentById(agentID);
		if (company != 0) {
			expresses = expressDao.getCompanyExpressBranch(company,agent);
			return "jsonSuccess";
		} else {
			return "success";
		}
	}

	public List<Expense> getExpenses() {
		return expenses;
	}

	public void setExpenses(List<Expense> expenses) {
		this.expenses = expenses;
	}

	public int getBranchID() {
		return branchID;
	}

	public void setBranchID(int branchID) {
		this.branchID = branchID;
	}

	public int getExpressId() {
		return expressId;
	}

	public void setExpressId(int expressId) {
		this.expressId = expressId;
	}

	public Expense getExpense() {
		return expense;
	}

	public void setExpense(Expense expense) {
		this.expense = expense;
	}

	public List<Express> getExpresses() {
		return expresses;
	}

	public void setExpresses(List<Express> expresses) {
		this.expresses = expresses;
	}

	public List<Company> getCompanies() {
		return companies;
	}

	public void setCompanies(List<Company> companies) {
		this.companies = companies;
	}

	public int getCompany() {
		return company;
	}

	public void setCompany(int company) {
		this.company = company;
	}

	public int getExpenseID() {
		return expenseID;
	}

	public void setExpenseID(int expenseID) {
		this.expenseID = expenseID;
	}

	public int getAgentID() {
		return agentID;
	}

	public void setAgentID(int agentID) {
		this.agentID = agentID;
	}

}
